package com.example.administrator.ygddriver.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.timeout.IdleStateHandler;
import io.netty.util.CharsetUtil;

public class TcpClient {
        public static String HOST = "47.92.117.222";
//    public static String HOST = "192.168.1.140";
    public static int PORT = 9999;
    public static Bootstrap bootstrap;
    private EventLoopGroup group;
    public ChannelFuture cf;

    private static class TcpClientHolder {
        //static final TcpClient instance = new TcpClient();
        static TcpClient instance = null;
    }

    public static TcpClient getInstance() {
        if (TcpClientHolder.instance == null) {
            TcpClientHolder.instance = new TcpClient();
        }
        return TcpClientHolder.instance;
    }

    public TcpClient() {
//		try{
//			group = new NioEventLoopGroup();
//		}catch (Exception e){
//			System.out.println("捕获异常。。。");
//		}
        System.setProperty("io.netty.noUnsafe", "true");
        group = new NioEventLoopGroup();
        bootstrap = new Bootstrap();
        bootstrap.group(group).channel(NioSocketChannel.class);
        bootstrap.handler(new ChannelInitializer<Channel>() {
            @Override
            protected void initChannel(Channel ch) throws Exception {
                ChannelPipeline pipeline = ch.pipeline();
                pipeline.addLast(new IdleStateHandler(5, 0, 0));
                pipeline.addLast("StringDecoder", new StringDecoder(CharsetUtil.UTF_8));
                pipeline.addLast("StringEncoder", new StringEncoder(CharsetUtil.UTF_8));
                pipeline.addLast("handler", new SimpleChatClientHandler());

            }
        });
        bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
        bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 2000);
    }


    public void connect() {
        try {
            this.cf = bootstrap.connect(HOST, PORT).sync();
            // this.channel = this.cf.channel();
            System.out.println("远程服务器已经连接, 可以进行数据交换.. 触发离线数据发送");
        } catch (Exception e) {
            //e.printStackTrace();
            System.out.println("获取通道失败。。。");
        }
    }

    public ChannelFuture getChannelFuture() {
        if (this.cf == null) {
            this.connect();
        } else {
            System.out.println("===============channel通道状态：" + this.cf.channel().isActive());
            if (!this.cf.channel().isActive()) {
                this.connect();
            }
        }

        return this.cf;
    }


    public static int sendMsgByByete(String ss) {
        TcpClient instance = TcpClient.getInstance();
        ChannelFuture cf = instance.getChannelFuture();
        if (cf != null && cf.channel().isActive()) {
            cf.channel().writeAndFlush(ss);

            return 1;
        } else {
            return 2;
        }
    }

    public static void sendMsgByByete() {
        TcpClient instance = TcpClient.getInstance();
        instance.getChannelFuture();
    }


    public ChannelFuture getCf() {
        return cf;
    }


    public static void main(String[] args) {
//		try {
//			TcpClient.sendMsgByByete("007BFF84010516014C414E4455303030303031000000027E38310057304C305A43463639333130383339314100323031352D30312D30372031303A35363A30360001382E370030003000453030302E3030303030302C4E30302E3030303030302C302C313937302D30312D30312030303A30303A31342C3000171B".getBytes());
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
        //boolean flag = TcpClient.getInstance().channel.isRegistered();
        //	System.out.println(flag);
    }
}
